package com.p7.notepad.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.p7.notepad.R;
import com.p7.notepad.app.BaseActivity;
import com.p7.notepad.app.TabFragmentPagerAdapter;
import com.p7.notepad.fragment.CollectFragment;
import com.p7.notepad.fragment.FileFragment;
import com.p7.notepad.fragment.MeFragment;
import com.p7.notepad.fragment.NewFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MainActivity extends BaseActivity {

    @BindView(R.id.main_pager)
    ViewPager mPager;
    @BindView(R.id.main_tab_layout)
    TabLayout mTabLayout;

    private TabFragmentPagerAdapter adapter;

    //ViewPage选项卡页面集合
    private List<Fragment> mFragments;

    private String[] titles = new String[]{"最近", "文件夹", "收藏", "我"};
    /**
     * 图片数组
     */
    private int[] mImgs = new int[]{R.drawable.tab_new, R.drawable.tab_file, R.drawable.tab_collect, R.drawable.tab_me};

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    int page = 0;
    @Override
    protected void initEvents() {
        super.initEvents();

        Bundle bundle = getIntent().getBundleExtra("bundle");
        if (bundle != null) {
            page = bundle.getInt("page");
        } else {
            page = 0;
        }

        mFragments = new ArrayList<>();
        mFragments.add(new NewFragment());
        mFragments.add(new FileFragment());
        mFragments.add(new CollectFragment());
        mFragments.add(new MeFragment());
        adapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), mFragments, titles);
        mPager.setAdapter(adapter);
        mTabLayout.setupWithViewPager(mPager);

        mTabLayout.setSelectedTabIndicatorHeight(0);
        for (int i = 0; i < titles.length; i++) {
            TabLayout.Tab itemTab = mTabLayout.getTabAt(i);
            if (itemTab != null) {
                itemTab.setCustomView(R.layout.tab_item);
                TextView textView =  itemTab.getCustomView().findViewById(R.id.tv_name);
                ImageView imageView = itemTab.getCustomView().findViewById(R.id.iv_img);
                textView.setText(titles[i]);
                imageView.setImageResource(mImgs[i]);
            }
        }
        mTabLayout.getTabAt(0).getCustomView().setSelected(true);

        mPager.setCurrentItem(page);
    }

    private long exitTime = 0;
    private static final int BACK_PRESSED_INTERVAL = 2000;
    @Override
    public void onBackPressed() {
        if (System.currentTimeMillis() - exitTime > BACK_PRESSED_INTERVAL) {
            exitTime = System.currentTimeMillis();
            Toast.makeText(this, "再按一次退出程序",
                    Toast.LENGTH_SHORT).show();
        } else {
            onExit();
        }
    }
}
